//    Copyright (C) Kherty.  All rights reserved.
#region

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.InteropServices;
using OpenLS.UI.Internals;

#endregion

namespace OpenLS.UI
{
    [StructLayout(LayoutKind.Sequential), TypeConverter(typeof (StatePriorityConverter))]
    public struct StatePriority : IEquatable<StatePriority>
    {
        private readonly double _value;
        private readonly bool _isEnabled;
        private static readonly StatePriority s_disabled;

        public StatePriority(double pixels) : this(pixels, true)
        {
        }

        public StatePriority(double value, bool isEnabled)
        {
            if (double.IsNaN(value))
            {
                throw new ArgumentException("value");
            }
            if (double.IsInfinity(value))
            {
                throw new ArgumentException("value");
            }
            _value = value;
            _isEnabled = isEnabled;
        }

        public static bool operator ==(StatePriority gl1, StatePriority gl2)
        {
            return ((gl1.IsEnabled == gl2.IsEnabled) && (gl1.Value == gl2.Value));
        }

        public static bool operator !=(StatePriority gl1, StatePriority gl2)
        {
            if (gl1.IsEnabled == gl2.IsEnabled)
            {
                return (gl1.Value != gl2.Value);
            }
            return true;
        }

        public override bool Equals(object oCompare)
        {
            if (oCompare is StatePriority)
            {
                var length = (StatePriority) oCompare;
                return (this == length);
            }
            return false;
        }

        public bool Equals(StatePriority gridLength)
        {
            return (this == gridLength);
        }

        public override int GetHashCode()
        {
            return (((int) _value) + IsEnabled.GetHashCode());
        }

        public double Value
        {
            get
            {
                if (IsEnabled)
                {
                    return _value;
                }
                // Contract.Assert(false);
                return double.NaN;
            }
        }

        public bool IsEnabled
        {
            get { return _isEnabled; }
        }

        public override string ToString()
        {
            return StatePriorityConverter.ToString(this, CultureInfo.InvariantCulture);
        }

        public static StatePriority Disabled
        {
            get { return s_disabled; }
        }

        public static StatePriority Never
        {
            get { return Disabled; }
        }

        static StatePriority()
        {
            s_disabled = new StatePriority(1.0, false);
        }

        public static StatePriority MaxValue
        {
            get { return new StatePriority(double.MaxValue); }
        }
    }
}